<?php
if (isset($_GET['sent_mail_id'])) {
	$sent_mail_id = intval($_GET['sent_mail_id']);

	// Lấy thông tin mail
	$sql = "SELECT sent_mail_id, subject, message, DATE_FORMAT(sent_date, '%H:%i:%s %d-%m-%Y') AS sent_date, attached_dir FROM sent_mail";
	$sql .= " WHERE sent_mail_id=" . $sent_mail_id;
	$result = mysql_query($sql) OR _log('Error query: ' . $sql);
	if ($result) {
		$sent_mail = mysql_fetch_assoc($result);
		$sent_mail['receiver'] = getReceivedContact($sent_mail_id);

		// Tệp đính kèm
		if ($sent_mail['attached_dir']) {
			$sent_mail['attached_files'] = glob(DIR_UPLOAD . 'attached_file/' . $sent_mail['attached_dir'] . '/*');			

			for ($i = 0; $i < count($sent_mail['attached_files']); $i++) {
				$sent_mail['attached_files'][$i] = basename($sent_mail['attached_files'][$i]);
			}
		} else {
			$sent_mail['attached_files'] = array();
		}

		/* Load template */
		if (file_exists(DIR_TPL . 'mail/detail.tpl.php')) {
			include(DIR_TPL . 'mail/detail.tpl.php');
		} else {
			_log('Could not load the template "mail/detail"!', true);
		}
	} else {
		echo 'Không tồn tại mail!';
	}
} else {
	header('Location: ' . URL . '?page=mail_da_gui');
}